package com.atguigu.service_statistics.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.RandomUtil;
import com.atguigu.service_statistics.client.UcenterClient;
import com.atguigu.service_statistics.entity.StatisticsDaily;
import com.atguigu.service_statistics.mapper.StatisticsDailyMapper;
import com.atguigu.service_statistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author clark
 * @since 2020-12-01
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;


    @Override
    public void registerCount(String day) {
        //添加记录之前删除相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("data_calculated",day);
        baseMapper.delete(wrapper);


        R r = ucenterClient.countRegister(day);
        Map<String, Object> data = r.getData();
        Integer countRegister = (Integer)data.get("countRegister");

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister);//注册人数
        statisticsDaily.setDateCalculated(day);

        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(statisticsDaily);


    }

    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> dailyList = baseMapper.selectList(wrapper);

        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        for (StatisticsDaily statisticsDaily : dailyList) {
            date_calculatedList.add(statisticsDaily.getDateCalculated());
            Integer num = 0;
            switch (type) {
                case "login_num":
                    num = statisticsDaily.getLoginNum();
                    break;
                case "register_num":
                    num = statisticsDaily.getRegisterNum();
                    break;
                case "video_view_num":
                    num = statisticsDaily.getVideoViewNum();
                    break;
                case "course_num":
                    num = statisticsDaily.getCourseNum();
                    break;
            }
            numDataList.add(num);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);

        return map;
    }
}
